package com.cloud.lan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.lan.api.dto.req.LanUserPraiseReq;
import com.cloud.lan.dao.LanGoodsEvaluateMapper;
import com.cloud.lan.dao.LanUserMapper;
import com.cloud.lan.dao.LanUserPraiseMapper;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.enums.LanError;
import com.cloud.lan.entity.model.dataobject.LanGoodsEvaluateDO;
import com.cloud.lan.entity.model.dataobject.LanUserDO;
import com.cloud.lan.entity.model.dataobject.LanUserPraiseDO;
import com.cloud.lan.entity.utils.VocUtil;
import com.cloud.lan.service.ILanUserPraiseService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ZERO;

/**
 * <p>
 * 用户点赞表 服务实现类
 * </p>
 *
 * @author xyqierkang@163.com
 * @since 2022-05-21
 */
@Slf4j
@Service
public class LanUserPraiseServiceImpl extends ServiceImpl<LanUserPraiseMapper, LanUserPraiseDO> implements ILanUserPraiseService {

    @Resource
    LanUserPraiseMapper lanUserPraiseMapper;
    @Resource
    LanUserMapper lanUserMapper;
    @Resource
    LanGoodsEvaluateMapper lanGoodsEvaluateMapper;

    @Override
    public BaseResponse manageList(LanUserPraiseReq req) {
        log.info("#查询 用户点赞 列表 {}", req);
        LambdaQueryWrapper<LanUserPraiseDO> qw = new LambdaQueryWrapper<>();
        qw.orderByDesc(LanUserPraiseDO::getCreateTime);
        Page<LanUserPraiseDO> page = new Page<>(req.getPageIndex(), req.getPageSize());
        if (req.getId() != null) {
            qw.eq(LanUserPraiseDO::getId, req.getId());
        }
        IPage<LanUserPraiseDO> pageList = lanUserPraiseMapper.selectPage(page, qw);
        pageList.getRecords().forEach(p -> {
            LanGoodsEvaluateDO lanGoodsEvaluateDO = lanGoodsEvaluateMapper.selectById(p.getEvaluateId());
            if (ObjectUtil.isNotEmpty(lanGoodsEvaluateDO)) {
                p.setGoodsName(lanGoodsEvaluateDO.getGoodsName());
            }
            LanUserDO lanUserDO = lanUserMapper.getUserId(p.getUserId());
            LanUserDO lanBeUserDO = lanUserMapper.getUserId(p.getBeUserId());
            p.setWxName(lanUserDO.getWxName());
            p.setWxHead(lanUserDO.getWxHead());
            p.setBeWxName(lanBeUserDO.getWxName());
            p.setBeWxHead(lanBeUserDO.getWxHead());
        });
        return BaseResponse.success(pageList);
    }

    @Override
    public BaseResponse add(LanUserPraiseReq req) {
        log.info("#新增 用户点赞 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getGoodsId()),
                        String.valueOf(req.getUserId()), String.valueOf(req.getBeUserId())},
                new String[]{"goodsId", "userId", "beUserId"}, LanError.ILLEGAL_PARAMS.getCode());
        LanUserPraiseDO lanUserPraiseDO = new LanUserPraiseDO();
        BeanUtil.copyProperties(req, lanUserPraiseDO);
        LambdaQueryWrapper<LanUserPraiseDO> luwQw = new LambdaQueryWrapper<>();
        luwQw.eq(LanUserPraiseDO::getUserId, lanUserPraiseDO.getUserId());
        luwQw.eq(LanUserPraiseDO::getBeUserId, lanUserPraiseDO.getBeUserId());
        luwQw.eq(LanUserPraiseDO::getCancel, ZERO.getKey());
        if (lanUserPraiseMapper.selectCount(luwQw) > ZERO.getKey()) {
            log.error("{}", LanError.LAN_010009.getMsg());
            return BaseResponse.success();
        }
        lanUserPraiseMapper.insert(lanUserPraiseDO);
        return BaseResponse.success();
    }
}
